package com.hqj.client.controller;

import com.hqj.client.entity.Classify;
import com.hqj.client.service.ClassifyService;
import com.hqj.common.JsonResult;
import com.hqj.common.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Api(tags = "博客分类模块")
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @PostMapping("/doInsertClassify")
    @ApiOperation("添加分类")
    @ResponseBody
    public JsonResult doInsertClassify(@RequestParam @ApiParam("分类名字")String name,
                                     @RequestParam @ApiParam("分类父级ID")Integer parentId,
                                     @RequestParam @ApiParam("分类类型")Integer type
    ){
        classifyService.doInsertClassify(name,parentId,type);
        return new JsonResult("添加成功");
    }

    @GetMapping("/doDeleteClassify")
    @ApiOperation("删除分类")
    @ResponseBody
    public JsonResult doDeleteClassify(@RequestParam @ApiParam("分类ID") Long id){
        classifyService.doDeleteClassify(id);
        return new JsonResult("删除成功");

    }

    @PostMapping("/doUpdateClassify")
    @ApiOperation("修改分类")
    @ResponseBody
    public JsonResult doUpdateClassify(@RequestBody @ApiParam("分类信息") Classify classify){
        classifyService.doUpdateClassify(classify);
        return new JsonResult("修改成功");
    }

    @GetMapping("/doSelectPage")
    @ApiOperation("分页查询分类")
    public JsonResult doSelectPage(@RequestParam @ApiParam("当前页码")int page,
                                 @RequestParam @ApiParam("每页显示记录数")int size,
                                 Model model){
        List<Classify> classifyList = classifyService.doSelectPage(page,size);
        model.addAttribute("classifyList",classifyList);
        return new JsonResult("查询成功");
    }

    @GetMapping("/doSelectByName")
    @ApiOperation("查询指定分类")
    @ResponseBody
    public JsonResult doSelectByName(@RequestParam @ApiParam("分类名字")String name,Model model){
        List<Classify> classifyList = classifyService.doSelectByName(name);
        System.out.println(classifyList);
        model.addAttribute("classifyListt",classifyList);
        return new JsonResult ("查询成功");
    }

}
